//
//  MLIBottomView.m
//  BaseProject
//
//  Created by mac on 2021/1/11.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "MLIBottomView.h"
#import "BaodanViewController.h"
#import "StoreCategoryViewController.h"
@interface MLIBottomView ()
@property(nonatomic,strong)UILabel *moneyLab;
@property(nonatomic,strong)UILabel *numerLab;
@property(nonatomic,strong)UIImageView *arrowIV;
@property(nonatomic,strong)UIButton *getButton;
//数据
@property(nonatomic,strong)NSString *insuranceUrl;
@property(nonatomic,strong)NSString *orderNum;
@end

@implementation MLIBottomView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = WhiteColor;
        
        self.moneyLab.text = @"";
        self.numerLab.text = @"";
        [self getButton];
    }
    return self;
}
- (void)setJsonData:(id)jsonData
{
    _jsonData = jsonData;
    NSString *insuranceUrl = [NSString stringWithFormat:@"%@",jsonData[@"data"][@"insuranceUrl"]];
    NSString *orderNum = [NSString stringWithFormat:@"%@",jsonData[@"data"][@"orderNum"]];
    //测试
    //insuranceUrl = @"";
    //orderNum = @"1";
    //测试
    self.insuranceUrl = insuranceUrl;
    self.orderNum = orderNum;

    if (insuranceUrl.length > 0 && orderNum.integerValue >= 2) {
        self.moneyLab.hidden = YES;
        self.numerLab.hidden = YES;
        self.arrowIV.hidden = YES;
        
        self.getButton.frame = CGRectMake(14*ADAPTER_WIDTH, 8*ADAPTER_WIDTH, self.width - 28*ADAPTER_WIDTH, 38*ADAPTER_WIDTH);
        self.getButton.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeHorizontal andWithSize:self.getButton.size andWithColors:@[(__bridge id)rgba(255, 146, 92, 1).CGColor,(__bridge id)rgba(255, 66, 66, 1).CGColor]];
        [self.getButton setTitle:@"查看我的保单" forState:UIControlStateNormal];
    } else {
        self.moneyLab.hidden = NO;
        self.numerLab.hidden = NO;
        self.arrowIV.hidden = NO;
        
        NSMutableAttributedString *titleAttributes = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",@"投保金额：52.0元"]];
        titleAttributes.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightMedium];
        titleAttributes.color = kColor51;
        [titleAttributes setFont:kFontRegular14 range:NSMakeRange(0, 5)];
        
        NSMutableAttributedString *titleAttributes1;
        if (orderNum.integerValue == 0) {
            titleAttributes1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"(%@/2完成两单立即赠送)  去下单",orderNum]];
            titleAttributes1.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
            titleAttributes1.color = rgba(255, 66, 66, 1);
            [titleAttributes1 setColor:rgba(136, 136, 136, 1) range:NSMakeRange(0, 13)];
            
            [titleAttributes1 setColor:rgba(51, 51, 51, 1) range:NSMakeRange(2, 2)];
        } else if (orderNum.integerValue == 1) {
            titleAttributes1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"(%@/2再完成一单就可以赠送)  去下单",orderNum]];
            titleAttributes1.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
            titleAttributes1.color = rgba(255, 66, 66, 1);
            [titleAttributes1 setColor:rgba(136, 136, 136, 1) range:NSMakeRange(0, 15)];
            
            [titleAttributes1 setColor:rgba(255, 66, 66, 1) range:NSMakeRange(1, 1)];
            [titleAttributes1 setColor:rgba(51, 51, 51, 1) range:NSMakeRange(2, 2)];
        } else {
            titleAttributes1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"(%@/2已完成两单，快去领取婚嫁保险吧)  ",orderNum]];
            titleAttributes1.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
            titleAttributes1.color = rgba(255, 66, 66, 1);
            [titleAttributes1 setColor:rgba(136, 136, 136, 1) range:NSMakeRange(0, 20)];
            
            self.arrowIV.hidden = YES;
            
            [titleAttributes1 setColor:rgba(255, 66, 66, 1) range:NSMakeRange(1, 1)];
            [titleAttributes1 setColor:rgba(51, 51, 51, 1) range:NSMakeRange(2, 2)];
        }
        
        self.moneyLab.attributedText = titleAttributes;
        self.numerLab.attributedText = titleAttributes1;
        
        self.getButton.frame = CGRectMake(self.width - 114*ADAPTER_WIDTH, 8*ADAPTER_WIDTH, 100*ADAPTER_WIDTH, 38*ADAPTER_WIDTH);
        self.getButton.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeHorizontal andWithSize:self.getButton.size andWithColors:@[(__bridge id)rgba(255, 146, 92, 1).CGColor,(__bridge id)rgba(255, 66, 66, 1).CGColor]];
        [self.getButton setTitle:@"立即领取" forState:UIControlStateNormal];
        
    }
}
#pragma mark - click
- (void)clickGetButton:sender
{
    if (self.insuranceUrl.length > 0 && self.orderNum.integerValue >= 2) {
        BaodanViewController *vc = [[BaodanViewController alloc]init];
        [self.parentVC.navigationController pushViewController:vc animated:YES];
    } else {
        [[TT_IMContactManager manager] ImContactWithType:TT_IMContactType_tongchoushi withQuickReply:@"您好，我想咨询下婚嫁保险问题。"];
    }
}
- (void)clickNumberLab:sender
{
    if (self.orderNum.integerValue < 2) {
        StoreCategoryViewController *vc = [[StoreCategoryViewController alloc]init];
        [self.parentVC.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - lazy懒加载
- (UILabel *)moneyLab
{
    if (!_moneyLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(14*ADAPTER_WIDTH);
            make.top.mas_equalTo(6*ADAPTER_WIDTH);
            make.height.mas_equalTo(22*ADAPTER_WIDTH);
        }];
        _moneyLab = lab;
    }
    return _moneyLab;
}
- (UILabel *)numerLab
{
    if (!_numerLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(14*ADAPTER_WIDTH);
            make.top.mas_equalTo(self.moneyLab.mas_bottom);
            make.height.mas_equalTo(17*ADAPTER_WIDTH);
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickNumberLab:)];
        lab.userInteractionEnabled = YES;
        [lab addGestureRecognizer:tap];
        _numerLab = lab;
    }
    return _numerLab;
}
- (UIButton *)getButton
{
    if (!_getButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button setTitle:@"立即领取" forState:UIControlStateNormal];
        [button setTitleColor:WhiteColor forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightMedium];
        [button addTarget:self action:@selector(clickGetButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(self.width - 114*ADAPTER_WIDTH, 8*ADAPTER_WIDTH, 100*ADAPTER_WIDTH, 38*ADAPTER_WIDTH);
        button.layer.cornerRadius = 19*ADAPTER_WIDTH;
        button.layer.masksToBounds = YES;
        button.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeHorizontal andWithSize:button.size andWithColors:@[(__bridge id)rgba(255, 146, 92, 1).CGColor,(__bridge id)rgba(255, 66, 66, 1).CGColor]];
        _getButton = button;
    }
    return _getButton;
}
- (UIImageView *)arrowIV
{
    if (!_arrowIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"MLI_Segement_bottom_arrow"];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        iv.clipsToBounds = YES;
        [iv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(4*ADAPTER_WIDTH, 7*ADAPTER_WIDTH));
            make.left.mas_equalTo(self.numerLab.mas_right).mas_equalTo(3*ADAPTER_WIDTH);
            make.centerY.mas_equalTo(self.numerLab);
        }];
        _arrowIV = iv;
    }
    return _arrowIV;
}
@end
